package com.tuniu.agents.common.message;

import com.tuniu.agents.common.enums.StreamMessageTypeEnum;
import com.tuniu.agents.common.message.adaper.AdapterAgentNewResp;
import com.tuniu.agents.common.message.adaper.AdapterRecQuestion;
import com.tuniu.agents.message.StreamMessage;
import com.tuniu.agents.util.JsonUtil;
import lombok.Data;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.StringUtils;

import java.util.List;

@Data
public class BaseAgentNewResp implements StreamMessage {


    /**
     * Success or error message (success defaults to empty string, error returns specific error message)
     */
    private String message = Strings.EMPTY;

    /**
     * Take requestId, unique identifier for each Q&A (also used for subsequent history retrieval)
     */
    private String responseId;

    /**
     * Session id, i.e. sessionId, placed in response header name Conversation_id
     */
    //private String conversationId;

    /**
     * Message type: analysis, answer, card
     */
    private String type;

    /**
     * All message content
     */
    private Object content;


//    private Card card; // Card data
//
//    private Analysis analysis; // Smart recommendations, intent analysis
//
//    private String answer; // Plain text message, e.g.: Based on your needs, here are the flights recommended for you:
//
//    private List<String> questionList; // Recommended questions

    private static final String recTitle = "Based on your needs, here are the recommendations for you:";

    // When type is answer type, content information can be used with this method
    private String getAnswerByCardType(String cardType) {

        if (StringUtils.isEmpty(cardType)) {
            return Strings.EMPTY;
        }

        for (CardTypeEnum cardTypeEnum : CardTypeEnum.values()) {
            if (cardTypeEnum.getType().equals(cardType)) {
                return recTitle + cardTypeEnum.getAnswer();
            }
        }

        return "Based on your needs, here are the recommended contents for you:";
    }


    public static BaseAgentNewResp getSuccessMsg(String questionId){
        BaseAgentNewResp baseAgentResp = new BaseAgentNewResp();
        baseAgentResp.setMessage("OK");
        baseAgentResp.setResponseId(questionId);
        return baseAgentResp;
    }
    public static BaseAgentNewResp getErrorMsg(String message,String questionId){
        BaseAgentNewResp baseAgentResp = new BaseAgentNewResp();
        baseAgentResp.setMessage(message);
        baseAgentResp.setResponseId(questionId);
        return baseAgentResp;
    }

    /**
     * Send error message
     * @param message Error message
     */
    public static String sendErrorMsg( String message, String questionId) {
        BaseAgentNewResp errorMsg = getErrorMsg( message,questionId);
        return errorMsg.toEventString();
    }

    /**
     * Send card information
     * @param data Card information
     * @param questionId questionId
     */
    public static String sendCardMsg(String cardType,List<Object> data,String answer,String questionId) {
        BaseAgentNewResp cardMsg = getSuccessMsg(questionId);
        cardMsg.setType(StreamMessageTypeEnum.CARD.getValue());
        cardMsg.setContent(new Card(cardType,data,answer));
        return AdapterAgentNewResp.sendCardMsg(cardMsg);
//        return cardMsg.toEventString();
    }

    /**
     * Send recommended questions
     * @param questionId
     */
    public static AdapterRecQuestion sendRecQuestion(String questionId, RecQuestionTypeEnum questionTypeEnum, String content) {
        BaseAgentNewResp recQuestion = getSuccessMsg(questionId);
        recQuestion.setType(StreamMessageTypeEnum.RECQUESTION.getValue());
        recQuestion.setContent(new RecQuestion(questionTypeEnum.getSubType(),content));
        return AdapterAgentNewResp.sendRecQuestion(recQuestion);
    }

    /**
     * Send analysis information
     * @param questionId questionId
     */
    public static String sendAnalysisMsg(String answer,int analysisDoneFlag, String questionId) {
        BaseAgentNewResp analysisMsg = getSuccessMsg(questionId);
        analysisMsg.setType(StreamMessageTypeEnum.ANALYSIS.getValue());
        analysisMsg.setContent(new Analysis("- " + answer + "\n",analysisDoneFlag));
        return AdapterAgentNewResp.sendAnalysisMsg(analysisMsg);
//        return analysisMsg.toEventString();
    }

    /**
     * Send content message
     * @param questionId
     */
    public static String sendAnswerMsg(String questionId,String answer) {
        BaseAgentNewResp answerMsg = getSuccessMsg(questionId);
        answerMsg.setType(StreamMessageTypeEnum.ANSWER.getValue());
        answerMsg.setContent(new Answer(answer));
        return AdapterAgentNewResp.sendAnswerMsg(answerMsg);
//        return answerMsg.toEventString();
    }

    /**
     * Send thinking information
     * @param questionId
     * @param answer
     */
    public static String sendThinkingMsg(String questionId,String answer) {
        BaseAgentNewResp answerMsg = getSuccessMsg(questionId);
        answerMsg.setType(StreamMessageTypeEnum.THINKING.getValue());
        answerMsg.setContent(new Answer(answer));
        return AdapterAgentNewResp.sendThinkingMsg(answerMsg);
    }

    /**
     * Send footer message
     * @param questionId
     * @param answer
     */
    public static String sendFooterMsg(String questionId,String answer) {
        BaseAgentNewResp answerMsg = getSuccessMsg(questionId);
        answerMsg.setType(StreamMessageTypeEnum.ANSWER.getValue());
        answerMsg.setContent(new Answer(answer));
        return AdapterAgentNewResp.sendFooterMsg(answerMsg);
    }

    /**
     * Send stop message
     * @param questionId
     * @param answer
     */
    public static String sendStopMsg(String questionId,String answer) {
        BaseAgentNewResp answerMsg = getSuccessMsg(questionId);
        answerMsg.setType(StreamMessageTypeEnum.STOP.getValue());
        answerMsg.setContent(new Answer(answer));
        return AdapterAgentNewResp.sendStopMsg(answerMsg);
    }

    /**
     * @param questionId
     * @param sessionId
     * @param card
     * @param analysis
     * @param answer
     * @param questionList
     * @return
     */
//    public static String sendFinishMsg(String questionId, String sessionId,Card card,Analysis analysis,String answer,List<String> questionList) {
//        BaseAgentNewResp successMsg = getSuccessMsg(questionId, sessionId);
//        successMsg.doneFlag = 1;
//        successMsg.setType("all");
//        successMsg.setContent(new Content()
//                .setAnalysis(analysis)
//                .setAnswer(answer)
//                .setCard(card)
//                .setQuestionList(questionList));
//        return successMsg.toEventString();
//    }

    /**
     * Builder static inner class
     */
    public static class Builder {

        private String message;

        private String responseId;

        private String type;

        private Object content;

        public Builder() {}


        public Builder message(String message) {
            this.message = message;
            return this;
        }

        public Builder responseId(String questionId) {
            this.responseId = questionId;
            return this;
        }

        public Builder type(String type) {
            this.type = type;
            return this;
        }

        public Builder content(Object content) {
            this.content = content;
            return this;
        }

        public BaseAgentNewResp build() {
            BaseAgentNewResp baseAgentResp = new BaseAgentNewResp();
            baseAgentResp.setMessage(message);
            baseAgentResp.setResponseId(responseId);
            baseAgentResp.setType(type);
            baseAgentResp.setContent(content);
            return baseAgentResp;
        }
    }

    /**
     * Convert object to JSON string representation
     */
    @Override
    public String toEventString() {
        return  " "+ JsonUtil.toJson(this);
    }
}
